<script setup>
import { ref, onMounted } from 'vue'
import _axios from '../../utils/myAxios.js'
import { ElMessage } from 'element-plus'

let accountAndArticle = ref({})
async function getAccountAndArticle() {
    const urlParam = new URLSearchParams(window.location.hash.split("?")[1])
    let id = urlParam.get('articleId')
    let resp = await _axios.get(`/api/account/getAccountAndArticle/${id}`)
    if (resp.data.code == 200) {
        accountAndArticle.value = resp.data.data
        if(accountAndArticle.value.avatar === null){
            accountAndArticle.value.avatar = 'http://47.92.174.180:8080/profile/2025/04/11/7a49c4e3c0ab4b059b187a60eefb2cfa.jpg'
        }
    } else {
        ElMessage({
            showClose: true,
            message: resp.data.message,
            type: 'error',
            appendTo: "#app",
        })
    }
}

onMounted(() => {
    getAccountAndArticle()
})

async function updateViewCount(id) {
    let resp = await _axios.put(`/api/article/updateViewCount/${id}`)
    if (resp.data.code !== 200) {
        ElMessage({
            showClose: true,
            message: resp.data.message,
            type: 'error',
            appendTo: "#app",
        })
    }
}

</script>

<template>
    <div class="contentRight">
        <el-card shadow="hover" v-if="accountAndArticle!==null">
            <div class="card">
                <div class="author">
                    关于作者
                </div>
                <div class="author-header">
                    <img :src="accountAndArticle.avatar">
                    <span>{{ accountAndArticle.nickname }}</span>
                </div>
                <div class="author-content">
                    <div class="author-content-item">
                        <div style="text-align: center;font-size: 14px;color: #8491A5;">文章数量</div>
                        <div class="content-count">{{ accountAndArticle.count }}</div>
                    </div>
                    <div class="author-content-item">
                        <div style="text-align: center;font-size: 14px;color: #8491A5;">作者最热</div>
                        <div class="content-article" v-for="(article, index) in accountAndArticle.articleVos"
                            :key="index">
                            {{ index + 1 }}、
                            <el-tooltip effect="light" :content="article.summary">
                                <a :href="'#/detailArticle?articleId=' + article.id"
                                        @click="updateViewCount(article.id)" target="_blank">
                                        {{ article.title }}
                                    </a>
                            </el-tooltip>
                            <span>---<span style="font-weight: 600;">{{ article.viewCount }}</span>次围观</span>
                        </div>
                    </div>
                </div>
            </div>
        </el-card>
    </div>
</template>

<style scoped>
.el-card {
    --el-card-padding: 0;
}

.card {
    margin-bottom: 0;
}

.author {
    border-bottom: 1px solid var(--el-border-color);
    padding: 15px;
    font-size: 14px;
    font-weight: 600;
}

.author-header {
    display: flex;
    align-items: center;
    padding: 15px;
    border-bottom: 1px solid var(--el-border-color);
}

.author-header img {
    border-radius: 2px;
    width: 50px;
}

.author-header span {
    font-weight: 600;
    font-size: 20px;
    margin-left: 5px;
}

.author-content {
    padding: 0 15px 15px 15px;
}

.author-content .author-content-item {
    margin-top: 20px;
}

.author-content .content-count {
    text-align: center;
    font-size: 16px;
    font-weight: 600;
    margin-top: 5px;
}

.content-article {
    margin: 10px 0;
}

.content-article a {
    font-size: 15px;
}
</style>